package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.qo.StatementQueryObject;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public TablePageInfo<Statement> query(StatementQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        Statement statement1 = new Statement();

        statement1.setCustomerName(statement.getCustomerName());
        statement1.setCustomerPhone(statement.getCustomerPhone());
        statement1.setActualArrivalTime(statement.getActualArrivalTime());
        statement1.setLicensePlate(statement.getLicensePlate());
        statement1.setCarSeries(statement.getCarSeries());
        statement1.setServiceType(statement.getServiceType());
        statement1.setInfo(statement.getInfo());
        statement1.setAppointmentId(statement.getAppointmentId());
        statement1.setCreateTime(new Date());

        statementMapper.insert(statement1);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectOne(id);
    }


    @Override
    public void update(Statement statement) {

        Statement statement1 = this.get(statement.getId());
        if (Statement.STATUS_PAID.equals(statement1.getStatus())) {
            throw new BusinessException("已支付,无法更改");
        }
        statement1.setCustomerName(statement.getCustomerName());
        statement1.setCustomerPhone(statement.getCustomerPhone());
        statement1.setActualArrivalTime(statement.getActualArrivalTime());
        statement1.setLicensePlate(statement.getLicensePlate());
        statement1.setCarSeries(statement.getCarSeries());
        statement1.setServiceType(statement.getServiceType());
        statement1.setInfo(statement.getInfo());

        statementMapper.update(statement1);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            Statement statement = this.get(dictId);
            statementMapper.delete(dictId);
            statementItemMapper.deleteBystatementId(dictId);
            if(statement.getAppointmentId() != null) {
                appointmentMapper.change(Appointment.STATUS_ARRIVAL,statement.getAppointmentId());
            }
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public void updateAmount(Statement statement) {
        statementMapper.updateAmount(statement);
    }

    @Override
    public void pay(Long statementId) {
        User user = ShiroUtils.getUser();
        Statement statement = this.get(statementId);
        statement.setPayeeId(user.getId());
        statement.setStatus(Statement.STATUS_PAID);
        statementMapper.pay(statement);
    }

    @Override
    public Statement getByAppointmentId(Long appointmentId) {
        return statementMapper.selectByappointmentId(appointmentId);
    }


}
